package com.bookstore.controller;

import com.alibaba.fastjson.JSON;

import com.bookstore.entity.Book;
import com.bookstore.entity.PageBean;
import com.bookstore.service.BookService;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author Cofe
 * @Date 2022/4/17 8:39
 */
@Controller
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Resource
    private BookService bookService;
    public static String Imgname;

    @RequestMapping(
            value = "/allbook",
            method = RequestMethod.GET
    )
    @CrossOrigin
    @ResponseBody
    public List<Book> findBookAll() {
        System.out.printf("查询图书");
        List<Book> bookList = bookService.findAllBook();
        return bookList;
    }

    @SneakyThrows
    @RequestMapping(
            value = "/insertbook",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    public String insertBook(@RequestParam("filename")MultipartFile file,String book_name, String book_type, String book_intro, int book_number,
                             double book_balance, double book_discount) {
        double book_membalance = book_balance * book_discount;  //折后价
        log.info("接收到的文件数据为：" + file);
        if (file.isEmpty()) {
            return "上传文件为空";
        }
        // 获取文件全名a.py
        String fileName = file.getOriginalFilename();
        // 文件上传路径
        String templatePath = "D:\\ChaoPei\\JavaBaseo\\WebAppp\\bookshop2.1\\src\\assets\\img";
        log.info("文件路径:" + templatePath);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //获取文件名
        String prefixName = fileName.substring(0, fileName.lastIndexOf("."));
        // 解决中文问题,liunx 下中文路径,图片显示问题
        //fileName = UUID.randomUUID() + suffixName;
        File dest0 = new File(templatePath);
        File dest = new File(dest0, File.separator + fileName);
        //文件上传-覆盖
        try {
            // 检测是否存在目录
            if (!dest0.getParentFile().exists()) {
                dest0.getParentFile().mkdirs();
                //检测文件是否存在
            }
            if (!dest.exists()) {
                dest.mkdirs();
            }
            file.transferTo(dest);
            bookService.saveBook(book_name, book_type, book_intro, book_number, book_balance, book_discount, book_membalance,prefixName);
            return "恭喜您添加图书成功";
        } catch (Exception e) {
            log.error("文件上传错误");
            return "上传失败";
        }
    }

    @RequestMapping(
            value = "/booktop10",
            method = RequestMethod.GET
    )
    @CrossOrigin
    @ResponseBody
    public List<Book> findbooktop() {
        List<Book> bookList = bookService.findBookTop();
        return bookList;
    }

    @SneakyThrows
    @RequestMapping(
            value = "/updatebook",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String updateBook(int bid, String book_name, String book_type, String book_intro,
                             int book_number, double book_balance, double book_discount) {
        Book book = bookService.findBookById(bid);
        if (book_name != "") {
            book.setBook_name(book_name);
        }
        if (book_type != "") {
            book.setBook_type(book_type);
        }
        if (book_intro != "") {
            book.setBook_intro(book_intro);
        }
        book.setBook_number(book_number);
        book.setBook_balance(book_balance);
        book.setBook_discount(book_discount);
        book.setBook_membalance(book_balance * book_discount);
        bookService.updateBook(book.getBid(), book.getBook_name(), book.getBook_type(), book.getBook_intro(), book.getBook_number(), book.getBook_balance(), book.getBook_discount(), book.getBook_membalance());

        return "恭喜您更新图书成功";
    }

    @RequestMapping(
            value = "/deletebook",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String deletebook(int bid) {
        String s = bookService.deleteBook(bid);
        return s;
    }

    @RequestMapping(
            value = "/selectbypage",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String selectByPage(int currentPage, int pageSize) {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        //2. 调用service查询
        PageBean<Book> pageBean = bookService.selectByPage(currentPage, pageSize);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        return jsonString;
    }


    /**
     * 分页条件查询
     *
     * @throws ServletException
     * @throws IOException
     */

    @RequestMapping(
            value = "/selectbypc",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String selectByPageAndCondition(int currentPage, int pageSize, @RequestBody Book selectbook) {
        //2. 调用service查询
        PageBean<Book> pageBean = bookService.selectByPageAndCondition(currentPage, pageSize, selectbook);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        return jsonString;
    }

    @RequestMapping(
            value = "/booktopbalance",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @ResponseBody
    @CrossOrigin
    public  String findbooktop(int currentPage,int pageSize) {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        //2. 调用service查询
        PageBean<Book> pageBean = bookService.findBookTopBalance(currentPage, pageSize);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        return jsonString;
    }

}
